
package main;

import core.COINCOMOUnit;
import java.awt.Color;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import extensions.COINCOMOFixedTable;

/**
 *
 * @author Raed Shomali
 */
public class GlobalMethods
{
    static Color defaultColor = COINCOMO.statusBar.getForeground();
    static String cotProject = "";

    /**
     * 
     * @param status is used to set the Status bar content
     */
    public static void updateStatusBar( String status )
    {
        COINCOMO.statusBar.setForeground( defaultColor );
        COINCOMO.statusBar.setText( " " + status.trim() );
    }

    /**
     * 
     * @param status is used to set the status bar content
     * @param color is used to set the status bar color
     */
    public static void updateStatusBar( String status , Color color )
    {
        COINCOMO.statusBar.setForeground( color );
        COINCOMO.statusBar.setText( " " + status.trim() );
    }

    /**
     * 
     * @param list is the Unordered Iterator
     * @return ordered Vector
     */
    public static Vector<COINCOMOUnit> getOrderedVector( HashMap list )
    {
        Vector<COINCOMOUnit> orderedVector = new Vector<COINCOMOUnit>();

        // Iterator Of the Hash map
        Iterator it = list.values().iterator();

        NextUnit:
        while ( it.hasNext() )
        {
            // Get Next Unit ...
            COINCOMOUnit unit = (COINCOMOUnit) it.next();

            for ( int i = 0 ; i < orderedVector.size() ; i++ )
            {
                // Temporary Unit For Comparison ..
                COINCOMOUnit tempUnit = (COINCOMOUnit) orderedVector.get( i );

                // If In Place ?
                if ( unit.getUnitID() < tempUnit.getUnitID() )
                {
                    // Insert it ...
                    orderedVector.insertElementAt( unit , i );

                    // Go Get the Next Unit ...
                    continue NextUnit;
                }
            }

            // Append to the end
            orderedVector.add( unit );
        }

        return orderedVector;
    }

    /**
     * 
     * @param value to be formatted
     * @param by determines the rounding
     * @return the formatted result
     */
    public static float roundOff( float value , int by )
    {
        float p = (float) Math.pow( 10 , by );

        value = value * p;

        float tempValue = Math.round( value );

        return (float) tempValue / p;
    }

    /**
     * 
     * @param value to be tested whether or not Non Negative Float
     * @return true if Non Negative, False otherwise
     */
    public static boolean isNonNegativeFloat( String value )
    {
        // To Check Duration Being Positive Integer
        try
        {
            double result = Float.parseFloat( value );

            // Value Must Be Nonnegative
            if ( result < 0 )
            {
                return false;
            }
        }
        catch ( Exception e )
        {
            return false;
        }

        return true;
    }

    /**
     * 
     * @param value to be tested whether or not to be of a Non Negative Long Value
     * @return true if Non Negative Long Value, false otherwise
     */
    public static boolean isNonNegativeLong( String value )
    {
        // To Check Duration Being Positive Integer
        try
        {
            long result = Long.parseLong( value );

            // Value Must Be Nonnegative
            if ( result < 0 )
            {
                return false;
            }
        }
        catch ( Exception e )
        {
            return false;
        }

        return true;
    }

    /**
     * 
     * @param text is the rating in String
     * @return corresponding rating value
     */
    public static int convertFromRating( String text )
    {
        if ( text.equals( "VLO" ) )
        {
            return 1;
        }
        else if ( text.equals( "LO" ) )
        {
            return 2;
        }
        else if ( text.equals( "NOM" ) )
        {
            return 3;
        }
        else if ( text.equals( "HI" ) )
        {
            return 4;
        }
        else if ( text.equals( "VHI" ) )
        {
            return 5;
        }
        else
        {
            return 6;
        }
    }

    /**
     * 
     * @param text is the increment percentage
     * @return the respective value
     */
    public static int convertFromPercent( String text )
    {
        if ( text.equals( "0%" ) )
        {
            return 0;
        }
        else if ( text.equals( "25%" ) )
        {
            return 1;
        }
        else if ( text.equals( "50%" ) )
        {
            return 2;
        }
        else
        {
            return 3;
        }
    }

    /**
     * 
     * @param i is used to determine the Rating
     * @return the rating
     */
    public static String convertToRating( int i )
    {
        switch( i )
        {
            case 1: return "VLO" ;
            case 2: return "LO" ;
            case 3: return "NOM" ;
            case 4: return "HI" ;
            case 5: return "VHI" ;
            default: return "NOM" ;
        }
    }
    
    public static double convertRatingToInteger( String rating )
    {
    	if(rating.equals("VLO"))
    		return 1;
    	else if (rating.equals("LO"))
    		return 2;
    	else if (rating.equals("NOM"))
    		return 3;
    	else if (rating.equals("HI"))
    		return 4;
    	else
    		return 5;
    	
    }
    
    public static String convertIntegerToRating( int rating )
    {
    	if(rating == 1 )
    		return "VLO";
    	else if ( rating == 2 )
    		return "LO";
    	else if ( rating == 3 )
    		return "NOM";
    	else if ( rating == 4 )
    		return "HI";
    	else
    		return "VHI";
    }
    
    public static String convertIntegerToRating( double rating )
    {
    	if(rating == 1 )
    		return "VLO";
    	else if ( rating == 2 )
    		return "LO";
    	else if ( rating == 3 )
    		return "NOM";
    	else if ( rating == 4 )
    		return "HI";
    	else
    		return "VHI";
    }
    
    public static int convertAssessRatingToInteger( String rating )
    {
    	if(rating.equals("U"))
    		return 1;
    	else if (rating.equals("EL"))
    		return 2;
    	else if (rating.equals("VLO"))
    		return 3;
    	else if (rating.equals("LO"))
    		return 4;
    	else if (rating.equals("NOM"))
    		return 5;
    	else if (rating.equals("HI"))
    		return 6;
    	else if (rating.equals("VHI"))
    		return 7;
    	else 
    		return 8;
    	
    }
    
    public static String convertAssessIntegerToRating( int rating )
    {
    	if(rating == 1 )
    		return "U";
    	else if ( rating == 2 )
    		return "EL";
    	else if ( rating == 3 )
    		return "VLO";
    	else if ( rating == 4 )
    		return "LO";
    	else if ( rating == 5 )
    		return "NOM";
    	else if ( rating == 6 )
    		return "HI";
    	else if ( rating == 7 )
    		return "VHI";
    	else
    		return "EHI";
    	
    }

    /**
     * 
     * @param i Used to determine the Increment Percentage
     * @return the percentage
     */
    public static String convertToPercent( int i )
    {
        switch( i )
        {
            case 0: return "0%" ;
            case 1: return "25%" ;
            case 2: return "50%" ;
            default: return "75%" ;
        }
    }
    
    public static int findColumnInTable ( String s, COINCOMOFixedTable clefTable )
    {
    	for(int i = 0; i < clefTable.getColumnCount(); i++)
    	{
    		if(clefTable.getColumnName(i).equals(s))
    			return i;
    	}
		return 0;
    	
    }
    
    public static String convertCotIdToString ( int i )
    {
    	if ( i == 1 )
			return "Generic Component";
    	if ( i == 2 )
			return "Back Office Retail";
    	if ( i == 3 )
			return "Communication protocols/packages";
    	if ( i == 4 )
			return "Compilers";
    	if ( i == 5 )
			return "Configuration mgmt/build tools";
    	if ( i == 6 )
			return "Data Conversion Packages";
    	if ( i == 7 )
			return "Databases";
    	if ( i == 8 )
			return "Device Drivers";
    	if ( i == 9 )
			return "Disk Arrays";
    	if ( i == 10 )
			return "Emulators";
    	if ( i == 11 )
			return "Engineering tools (req mgmt, design)";
    	if ( i == 12 )
			return "Graphical Information System";
    	if ( i == 13 )
			return "GUIs/GUI builders";
    	if ( i == 14 )
			return "Middleware";
    	if ( i == 15 )
			return "Network Managers";
    	if ( i == 16 )
			return "Operating Systems";
    	if ( i == 17 )
			return "Problem mgmt";
    	if ( i == 18 )
			return "Report Generators";
    	if ( i == 19 )
			return "Software Process Tools";
    	if ( i == 20 )
			return "Telecommunication & Infrastructure";
    	if ( i == 21 )
			return "Telemetry Analysis";
    	if ( i == 22 )
			return "Telemetry Processing";
    	if ( i == 23 )
			return "Word Processing";
    	else 
    		return "Aggregate Glue Code";
    }
}
